package com.shengde.jinwawaforleader.model.personalCenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.activity.MainActivity;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.base.CommonBaseAdapter;
import com.shengde.jinwawaforleader.base.ViewHolder;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProKingardenList;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

import static com.shengde.jinwawaforleader.HomeApplication.showToastShort;

/**
 * Created by Administrator on 2016/9/7 0007.
 */
public class KingardenListActivity extends BaseActivity implements AdapterView.OnItemClickListener {
	private static final int FINSH = 1;
	private static final String TAG = "KingardenListActivity";
	private TextView tv_back;
	private TextView tv_title;
	private ListView lv_kingraden_list;
	private List<ProKingardenList.InfoBean> info_bean; //返回的幼儿园的名字信息
	private CommonBaseAdapter kingarden_list_activity_adapter;//幼儿园列表adapter
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case FINSH:
					if (info_bean.size() ==0){
						showToastShort(getString(R.string.no_more_kingarden));
						return;
					}
					if (kingarden_list_activity_adapter == null) {
						kingarden_list_activity_adapter = new CommonBaseAdapter(KingardenListActivity.this, info_bean, R.layout.kingardenlist_item) {
							@Override
							public void convert(ViewHolder holder, final int position) {
								holder.setText(R.id.tv_school_name,info_bean.get(position).getName());

								holder.getView(R.id.tv_change).setOnClickListener(new View.OnClickListener() {
									@Override
									public void onClick(View v) {
										User.setSchoolId(KingardenListActivity.this,info_bean.get(position).getId() +"");
										User.setSchoolName(KingardenListActivity.this,info_bean.get(position).getName());
										showToastShort(getString(R.string.change_success));
										finish();
//										getIntentCallBack().startActivityFinishCommon(MainActivity.class);

									}
								});
							}
						};
						lv_kingraden_list.setAdapter(kingarden_list_activity_adapter);
					} else {
						kingarden_list_activity_adapter.notifyDataSetChanged();
					}
					break;
			}
		}
	};

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.kingarden_list_activity);
	}

	@Override
	public void initView() {
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		lv_kingraden_list = (ListView) findViewById(R.id.lv_kingraden_list);
	}

	@Override
	public void initData() {
		tv_title.setText(getString(R.string.kingarden_list));
		showKingradenList();
	}

	@Override
	public void initListener() {
		tv_back.setOnClickListener(this);
		lv_kingraden_list.setOnItemClickListener(this);
	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

	}

	/**
	 * 请求幼儿园列表
	 */
	private void showKingradenList() {
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/director/schoolList";
		} else {
			url = BaseProtocol.HOST_BASE + "/director/schoolList";
		}
		//http://120.76.155.123:8080/api/director/directorLogiin?phone=three&pwd=three
		RequestParams params  = BaseProtocol.getRequestParams(url);
		params.addParameter("schoolid", User.getSchoolId(this));
		Log.i(TAG, "showKingradenList: " + User.getSchoolId(this));
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProKingardenList pro_kingarden_list = new Gson().fromJson(result, ProKingardenList.class);
				if (pro_kingarden_list != null && pro_kingarden_list.getCode() == 0) {
					info_bean = pro_kingarden_list.getInfo();
					Log.i(TAG, "onSuccess: " + info_bean.size());
					mHandler.sendEmptyMessage(FINSH);
				}
			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {

			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {

			}
		});
	}
}
